package com.monkey.utils.upload.impl;

import com.monkey.utils.MonkeyUtils;
import com.monkey.utils.upload.FileService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Wu Tian Qiang
 */
public class LocalFileServiceImpl implements FileService, ServletContextAware {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    private ServletContext context;
    private String prePath = "";

    @Override
    public String temp(MultipartFile file, String basePath) throws IOException {

        String name = generateUUIDName(getExt(file.getOriginalFilename()));
        String path = basePath + "/" + name;
        File temp = new File(getRoot() + path);
        checkDirAndCreate(temp);
        file.transferTo(temp);
        return getPrePath() + path;
    }


    @Override
    public String store(MultipartFile file, String basePath) throws IOException {
        String name = generateUUIDName(getExt(file.getOriginalFilename()));

        String filePath = basePath + "/" + name;
        File temp = new File(getRoot() + filePath);
        checkDirAndCreate(temp);
        file.transferTo(temp);
        return filePath;
    }

    @Override
    public String store(File file, String basePath) throws IOException {
        String name = generateUUIDName(getExt(file.getName()));

        String filePath = basePath + "/" + name;
        File dest = new File(getRoot() + filePath);
        checkDirAndCreate(dest);
        FileUtils.copyFile(file, dest);
        return filePath;
    }


    @Override
    public String store(String tempFilePath, String basePath) throws IOException {
        File tempFile = new File(getRoot() + tempFilePath);
        return store(tempFile, basePath);
    }

    @Override
    public void deleteFile(String storePath) {
        File file = new File(getRoot() + storePath);

        // 文件存在, 且不是目录
        if (file.exists() && !file.isDirectory()) {
            file.delete();
            log.info("fileRepo delete " + storePath);
        }
    }

    @Override
    public boolean checkFileType(String fileName, List<String> allowFiles) {
        for (String ext : allowFiles) {
            if (fileName.toLowerCase().endsWith(ext)) {
                return true;
            }
        }
        return false;
    }

    protected String generateUUIDName(String ext) {
        return MonkeyUtils.uuid() + "." + ext;
    }

    protected String getExt(String filename) {
        int pos = filename.lastIndexOf(".");
        return filename.substring(pos + 1);
    }

    protected void checkDirAndCreate(File file) {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
    }

    @Override
    public String getRoot() {
        return context.getRealPath("/") + getPrePath();
    }


    @Override
    public void setServletContext(ServletContext servletContext) {
        this.context = servletContext;
    }

    public String getPrePath() {
        return prePath;
    }

    public void setPrePath(String prePath) {
        this.prePath = prePath;
    }
}
